package com.lxmes.hr.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.hr.domain.NcOriginalAttend;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.hr.domain.NcAttendDetail;
import com.lxmes.hr.service.INcAttendDetailService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考勤明细Controller
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@RestController
@RequestMapping("/hr/attendDetail")
public class NcAttendDetailController extends BaseController
{
    @Autowired
    private INcAttendDetailService ncAttendDetailService;

    /**
     * 查询考勤明细列表
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcAttendDetail ncAttendDetail)
    {
        startPage();
        List<NcAttendDetail> list = ncAttendDetailService.selectNcAttendDetailList(ncAttendDetail);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:list')")
    @GetMapping("/getAttendTjDetail/{type}")
    public AjaxResult getAttendTjDetail(@PathVariable("type") String type)
    {
        return AjaxResult.success(ncAttendDetailService.attendanceStatistics(type));
    }

    /**
     * 导出考勤明细列表
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:export')")
    @Log(title = "考勤明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcAttendDetail ncAttendDetail)
    {
        List<NcAttendDetail> list = ncAttendDetailService.selectNcAttendDetailList(ncAttendDetail);
        ExcelUtil<NcAttendDetail> util = new ExcelUtil<NcAttendDetail>(NcAttendDetail.class);
        util.exportExcel(response, list, "考勤明细数据");
    }

    /**
     * 获取考勤明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncAttendDetailService.selectNcAttendDetailById(id));
    }

    /**
     * 新增考勤明细
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:add')")
    @Log(title = "考勤明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcAttendDetail ncAttendDetail)
    {
        return toAjax(ncAttendDetailService.insertNcAttendDetail(ncAttendDetail));
    }

    /**
     * 修改考勤明细
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:edit')")
    @Log(title = "考勤明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcAttendDetail ncAttendDetail)
    {
        return toAjax(ncAttendDetailService.updateNcAttendDetail(ncAttendDetail));
    }

    /**
     * 删除考勤明细
     */
    @PreAuthorize("@ss.hasPermi('hr:attendDetail:remove')")
    @Log(title = "考勤明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncAttendDetailService.deleteNcAttendDetailByIds(ids));
    }
    @Log(title = "原始考勤表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcAttendDetail> util = new ExcelUtil<>(NcAttendDetail.class);
        List<NcAttendDetail> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncAttendDetailService.imporIteminfo(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<NcAttendDetail> util = new ExcelUtil<>(NcAttendDetail.class);
        util.importTemplateExcel(response, "原始考勤表数据");
    }
}
